package com.prs.crm.action.pm;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.crm.CustomerLevel;
import com.prs.crm.domain.pm.PmStageType;
import com.prs.crm.service.pm.PmStageTypeService;
import com.prs.crm.service.sys.LogService;

public class PmStageTypeAction extends BaseAction {
	private PmStageType pmStageType;
	private PmStageTypeService pmStageTypeService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private LogService logService;

	@Action("listPmStageType")
	public String listPmStageType() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				PmStageType.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmStageTypeService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}

	@Action("editPmStageType")
	public String editPmStageType() {
		return SUCCESS;
	}

	@Action(value = "submitPmStageType", results = {
			@Result(name = "success", type = "redirect", location = "listPmStageType"),
			@Result(name = "error", type = "chain", location = "editPmStageType") })
	public String submitPmStageType() {
		if (pmStageType.getName() == null
				|| "".equals(pmStageType.getName().trim())) {
			addActionError("名称不能为空");
			return ERROR;
		}
		if (this.getLogService().getSearchName(PmStageType.class,
				pmStageType.getName())) {
			this.addActionError("名称已经存在");
			return ERROR;
		}
		this.getPmStageTypeService().save(pmStageType);
		return SUCCESS;
	}

	@Action(value = "removePmStageType", results = {
			@Result(name = "success", type = "redirect", location = "listPmStageType"),
			@Result(name = "error", type = "chain", location = "listPmStageType") })
	public String removePmStageType() {
		if (ids == null) {
			this.addActionError("请选择要删除的阶段类型");
			return ERROR;
		}
		for (Integer id : ids) {
			PmStageType type = this.getPmStageTypeService().get(id);
			if (type.getStages().size() != 0) {
				this.addActionError("已有阶段引用，不能删除");
				return ERROR;
			}
		}
		this.getPmStageTypeService().batchRemove(ids);
		return SUCCESS;
	}

	public PmStageType getPmStageType() {
		return pmStageType;
	}

	public void setPmStageType(PmStageType pmStageType) {
		this.pmStageType = pmStageType;
	}

	public PmStageTypeService getPmStageTypeService() {
		return pmStageTypeService;
	}

	public void setPmStageTypeService(PmStageTypeService pmStageTypeService) {
		this.pmStageTypeService = pmStageTypeService;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public Integer[] getIds() {
		return ids;
	}

	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public LogService getLogService() {
		return logService;
	}

	public void setLogService(LogService logService) {
		this.logService = logService;
	}
}
